"""Add quality scoring to leads table.

Revision ID: 20250120_add_quality_scoring
Revises: 20250118_add_follow_up_reminders
Create Date: 2025-01-20 10:00:00.000000

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '20250120_add_quality_scoring'
down_revision = '20250118_add_follow_up_reminders'
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Add quality scoring fields to leads table."""
    # 添加质量评分字段
    op.add_column('leads', sa.Column('quality_score', sa.Integer(), nullable=True, comment='客户质量分数（0-100）：基于5维度综合评分'))
    op.add_column('leads', sa.Column('quality_grade', sa.String(1), nullable=True, comment='客户质量等级：S(90+)/A(80+)/B(70+)/C(60+)/D(<60)'))
    op.add_column('leads', sa.Column('quality_updated_at', sa.DateTime(), nullable=True, comment='质量评分最后更新时间'))

    # 创建索引以提升查询性能
    op.create_index('ix_leads_quality_score', 'leads', ['quality_score'])
    op.create_index('ix_leads_quality_grade', 'leads', ['quality_grade'])


def downgrade() -> None:
    """Remove quality scoring fields from leads table."""
    op.drop_index('ix_leads_quality_grade', table_name='leads')
    op.drop_index('ix_leads_quality_score', table_name='leads')
    op.drop_column('leads', 'quality_updated_at')
    op.drop_column('leads', 'quality_grade')
    op.drop_column('leads', 'quality_score')
